﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;

namespace QuanLyNhaSach.Book
{
    public partial class FrmCategoryDetail : DevComponents.DotNetBar.Office2007Form
    {
        CategoryBUS categoryBus;

        public ActionName Action { get; set; }
        public CategoryDTO Category { get; set; }

        #region Constructor

        public FrmCategoryDetail()
        {
            InitializeComponent();
            categoryBus = new CategoryBUS();
            btnUpdate.Location = btnAdd.Location;
        }

        #endregion Constructor

        #region Private Methods

        private void CategoryDetail_Load(object sender, EventArgs e)
        {
            if (Action == ActionName.Insert)
            {
                DocSequenceBUS docsequenceBus = new DocSequenceBUS();
                txtCategoryCode.Text = docsequenceBus.GetNextDocSequenceNumber(DocSequence.Category.ToString());
                txtCategoryName.Focus();
                Category = new CategoryDTO();
            }
            else
            {
                txtCategoryCode.Text = Category.CategoryCode;
                txtCategoryName.Text = Category.CategoryName;
            }

            ProcessButton();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            Category.CategoryCode = txtCategoryCode.Text;
            Category.CategoryName = txtCategoryName.Text;

            if (!CheckDataInput())
            {
                return;
            }

            Category.CategoryID = categoryBus.InsertCategory(Category);
            if (Category.CategoryID > 0)
            {
                Global.SetMessage(lblMessage, "Thêm thành công!", true);
                Action = ActionName.Update;
                ProcessButton();

                Book.FrmCategoryList frmList = (Book.FrmCategoryList)Application.OpenForms["FrmCategoryList"];
                if (frmList != null)
                {
                    frmList.CategoryChangeAfterInsert(Category);
                }
                //OnCategoryChanged(Category);
            }
            else
            {
                Global.SetMessage(lblMessage, "Thêm không thành công!", false);
            }
        }

        private bool CheckDataInput()
        {
            if (string.IsNullOrEmpty(txtCategoryCode.Text) || string.IsNullOrEmpty(txtCategoryName.Text))
            {
                Global.SetMessage(lblMessage, "Tên thể loại không được trống!", false);
                txtCategoryName.Focus();
                return false;
            }
            return true;
        }

        public void ResetForm()
        {
            DocSequenceBUS docSeqBUS = new DocSequenceBUS();
            txtCategoryName.Text = string.Empty;
            txtCategoryCode.Text = docSeqBUS.GetNextDocSequenceNumber(DocSequence.Category.ToString());
            txtCategoryName.Focus();
        }

        #region Button

        private void btnAddNew_Click(object sender, EventArgs e)
        {
            ResetForm();
            Action = ActionName.Insert;
            ProcessButton();
            Category = new CategoryDTO();
        }

        private void ProcessButton()
        {
            if (Action == ActionName.Insert)
            {
                btnUpdate.Visible = false;
                btnAdd.Visible = true;
            }
            else if (Action == ActionName.Update)
            {
                btnUpdate.Visible = true;
                btnAdd.Visible = false;
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if(!CheckDataInput())
            {
                return;
            }

            Category.CategoryName = txtCategoryName.Text;

            if (categoryBus.UpdateCategory(Category))
            {
                Global.SetMessage(lblMessage, "Cập nhật thành công!", true);
                //OnCategoryChanged(Category);
                Book.FrmCategoryList frmList = (Book.FrmCategoryList)Application.OpenForms["FrmCategoryList"];
                if (frmList != null)
                {
                    frmList.CategoryChanged(Category);
                }
            }
            else
            {
                Global.SetMessage(lblMessage, "Cập nhật không thành công!", false);
            }
        }

        #endregion Button

        private void CategoryDetail_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                switch (Action)
                {
                    case ActionName.Insert:
                        btnAdd_Click(sender, e);
                        break;
                    case ActionName.Update:
                        btnUpdate_Click(sender, e);
                        break;
                }
            }
        }

        #endregion Private Methods

        //public delegate void CategoryHasChanged(CategoryDTO cateDTO);
        //public event CategoryHasChanged OnCategoryChanged;
    }
}
